<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 树形 -->
        <el-tree
          :data="data"
          node-key="id"
          icon-class="aaa"
          default-expand-all
          expand-on-click-node
        >
          <template slot-scope="{ data, node }" class="custom-tree-node">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
              <el-col :span="20">
                <svg-icon v-if="data.children.length===0" icon-class="peo" />
                <svg-icon v-else :icon-class="node.expanded ? 'jianhao' : 'add'" />
                <span> {{ data.name }} </span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <!-- 弹出层 -->
    <el-dialog
      v-if="showDialog"
      :title="isEdit ? '编辑' : '添加'"
      :visible.sync="showDialog"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <addor-edit :id="id" :origin-list="originList" :is-edit="isEdit" @close="close" @closeDia="closeDia" />
    </el-dialog>
  </div>
</template>

<script>
import { getDepartments, delDepartmentsById } from '@/api/getDepartments'
import { tranListToTreeData } from '@/utils/index.js'
import AddorEdit from './deptDialog.vue'
export default {
  components: {
    AddorEdit
  },
  data() {
    return {
      // 依赖一份树形数据
      data: [],
      showDialog: false,
      id: '',
      isEdit: false,
      originList: []
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      const res = await getDepartments()
      console.log(res)
      res.data.depts.shift()
      this.originList = res.data.depts.map(({ id, pid, code, name }) => {
        return { id, name, code, pid }
      })
      this.data = tranListToTreeData(res.data.depts)
    },
    hAdd(id) {
      this.isEdit = false
      this.showDialog = true
      this.id = id
    },
    close() {
      this.showDialog = false
      this.getDepartments()
    },
    hEdit(id) {
      this.isEdit = true
      this.showDialog = true
      this.id = id
    },
    async loadDelDepartments(id) {
      await delDepartmentsById(id)
      this.getDepartments()
    },
    hDel(id) {
      this.$confirm('你确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loadDelDepartments(id)
      }).catch(() => {
        // 用户取消退出
      })
    },
    closeDia() {
      this.showDialog = false
    }
  }
}
</script>
